package org.jaaksi.view.tabview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

/**
 * Created by Fcy on 2016/6/18.
 */
public class TabView extends RadioGroup
    implements RadioGroup.OnCheckedChangeListener {
    private int mSpacing = 0;
    private int mNum = 2;
    private int width = AUTO_WIDTH;
    private int height = ViewGroup.LayoutParams.MATCH_PARENT;
    private int mResId;
    private int mSelectPos = -1;
    private static final int AUTO_WIDTH = 0;

    // private long mFromTime;
    // private long mEndTime;

    /**
     * 是否允许点击改变星星状态
     */
    private boolean isClickEnable = true;

//    private List<RadioButton> list = new ArrayList<>();

    public TabView(Context context, AttributeSet attrs) {
        super(context, attrs);
        int currPos = 0;

        if (attrs != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs,
                R.styleable.TabView);
            isClickEnable = typedArray
                .getBoolean(R.styleable.TabView_click_enable, true);
            mNum = typedArray.getInteger(R.styleable.TabView_tab_num, 1);
            currPos = typedArray.getInteger(R.styleable.TabView_checked_pos, 0);
            mSpacing = typedArray
                .getDimensionPixelSize(R.styleable.TabView_space, 1);
            mResId = typedArray
                .getResourceId(R.styleable.TabView_tab_background, -2);
            try {
                width = typedArray.getDimensionPixelSize(
                    R.styleable.TabView_tab_width, AUTO_WIDTH);
            } catch (Exception ex) {
                width = AUTO_WIDTH;
            }

            try {
                height = typedArray.getDimensionPixelSize(
                    R.styleable.TabView_tab_height, 0);
            } catch (Exception ex) {
                height = ViewGroup.LayoutParams.MATCH_PARENT;
            }

            typedArray.recycle();
        }
        initView();
        setSelectPos(currPos);
    }

    private void initView() {
        setOrientation(HORIZONTAL);
        setOnCheckedChangeListener(this);
        removeAllViews();
        // list = new ArrayList<radioButton>()
        for (int i = 0; i < mNum; i++) {
            RadioButton radioButton = new RadioButton(getContext());
            radioButton.setGravity(Gravity.CENTER);
            radioButton.setTextColor(getResources()
                .getColorStateList(R.color.radio_btn_text_selector));
//            list.add(radioButton);

            if (!isClickEnable) {
                radioButton.setClickable(false);
            }
            LayoutParams params = new LayoutParams(width, height);
            if (i == 0) {
                params.leftMargin = 0;
            } else {
                params.leftMargin = mSpacing;
            }
            // 每个tab宽度等分TabView剩余宽度（除去space间距）
            if (width == AUTO_WIDTH) {
                params.weight = 1;
            }

            if (mResId != -2) {
                // 要设置文字，必须使用这种方式
                radioButton.setBackgroundResource(mResId);
                // radioButton.setButtonDrawable(null); // 这么设置是无效的
                radioButton.setButtonDrawable(new ColorDrawable());
            }

            addView(radioButton, params);

        }
    }

    /**
     * tab文案数组
     */
    private String[] mContentArr;

    private RadioButton getRadioButton(int pos) {
        return (RadioButton) getChildAt(pos);
    }

    /**
     * 选择选中的tab
     *
     * @param position
     */
    public void setSelectPos(int position) {
        if (position > mNum)
            return;
        if (mSelectPos != position) {
            getRadioButton(position).setChecked(true);
        }
    }

    /**
     * @return 当前选中的tab
     */
    public int getSelectPos() {
        return mSelectPos;
    }

    /**
     * 初始化文案
     *
     * @param contentArr tab文案数组
     */
    public void initContentArr(String[] contentArr) {
        mContentArr = contentArr;
        if (mContentArr != null && mContentArr.length >= mNum) {
            for (int i = 0; i < mNum; i++) {
                getRadioButton(i).setText(contentArr[i]);
            }
        }
    }

    private OnTabChangeListener mOnTabChangeListener;

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        for (int i = 0; i < mNum; i++) {
            if (getRadioButton(i).isChecked()) {
                mSelectPos = i;
                break;
            }
        }
        if (mOnTabChangeListener != null) {
            mOnTabChangeListener.onTabChange(mSelectPos);
            // mOnTabChangeListener.getSelectTime();
        }
    }

    public void setOnTabChangeListener(OnTabChangeListener onTabChangeListener) {
        mOnTabChangeListener = onTabChangeListener;
    }

    public interface OnTabChangeListener {
        void onTabChange(int position);
    }

}
